package cn.doourbest.learnspring.tomcat;

import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.auth.AuthState;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @description:
 * @author: zhangsc
 * @create: 2019/10/31
 */
public class MmainThreadPoolExecutor {

    private static ThreadPoolExecutor comitTaskPool = new ThreadPoolExecutor(5, 10, 1,
            TimeUnit.MINUTES, new
            ArrayBlockingQueue<Runnable>(30), new ThreadPoolExecutor.AbortPolicy ());

    private static int a = 0 ;

    public static void main(String[] args) throws InterruptedException, IOException {


        //System.out.println("ccc--------------"+(System.currentTimeMillis() -aaa));
//       comitTaskPool.execute(()->{
//
//            while(true){
//                try {
//                    Thread.sleep(50L);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//                System.out.println("---");
//                System.out.println("jihuo---"+comitTaskPool.getActiveCount());
//                System.out.println("yiwancheng---"+comitTaskPool.getCompletedTaskCount());
//                System.out.println("cole----"+comitTaskPool.getPoolSize());
//                System.out.println("suoding---" + comitTaskPool.getQueue().size());
//                System.out.println("---");
//            }
//        });

        while (true){
            Thread.sleep(200);
            long aaa = System.currentTimeMillis();
            System.out.println(sendMessage());
            System.out.println("yongshi------------" + (System.currentTimeMillis() - aaa));
        }

    }

    private static String sendMessage(){

        a++;
        long aa = System.currentTimeMillis();

        try {
            comitTaskPool.execute( ()->{
                long aaa = System.currentTimeMillis();
                try {
                    HttpClient httpClient = new HttpClient();
                    System.out.println(1);
                    httpClient.getHttpConnectionManager().getParams()
                            .setConnectionTimeout(3000);
                    HttpMethod method = new PostMethod("http://www.google.com/");
                    httpClient.executeMethod(method);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                System.out.println("====================" + (System.currentTimeMillis() - aaa));
            });
        } catch (Exception e) {
            //e.printStackTrace();
        }

        System.out.println("yongshi ==" + (System.currentTimeMillis() - aa));

       return "success";
    }
}
